package dai.core.base

import java.lang.Float.max
import java.lang.Float.min
import java.util.concurrent.TimeUnit

object Utils {

    private const val _TO_RADIUS_ = (Math.PI / 180.0).toFloat()
    private const val _TO_DEGRESS_ = (180.0 / Math.PI).toFloat()

    // 读取文本
    fun loadShader(filename: String): String = Utils::class.java.getResource(filename).readText(Charsets.UTF_8)

    fun toRadius(v: Float): Float = _TO_RADIUS_ * v
    fun toDegress(rad: Float): Float = _TO_DEGRESS_ * rad;

    fun randomInt(minValue: Int, maxValue: Int): Int {
        val d = maxValue - minValue + 1
        return (d * Math.random()).toInt() + minValue
    }

    fun arrayListToArray(arr: ArrayList<Float>): FloatArray {
        val floatArray = FloatArray(arr.size)
        arr.forEachIndexed { index, v ->
            floatArray[index] = v
        }
        return floatArray
    }

    fun arrayListToArray(arr: ArrayList<Int>): IntArray {
        val intArr = IntArray(arr.size)
        arr.forEachIndexed { index, v ->
            intArr[index] = v
        }
        return intArr
    }

    fun readAllLines(fileName: String): List<String> =
        Utils::class.java.getResource(fileName).readText(Charsets.UTF_8).split("\n")

    fun clampFloat(value: Float, minValue: Float, maxValue: Float) = max(minValue, min(value, maxValue))
    fun clampInt(value: Int, minValue: Int, maxValue: Int) = Math.max(minValue, Math.min(value, maxValue))


    fun mapFloat(valueInA: Float, fromA: Float, endA: Float, fromB: Float, endB: Float): Float {
        val distA = endA - fromA
        val distB = endB - fromB
        return fromB + distB * (valueInA - fromA) / distA
    }

    fun curMs() = TimeUnit.MILLISECONDS.convert(System.nanoTime(),TimeUnit.NANOSECONDS)
}

//
//fun main(){
//    println(Utils.mapFloat(100f,0f,200f,0f,1f))
//}